#include "set_translation.h"
#include "ui_set_translation.h"
#include<qdebug.h>

set_translation::set_translation(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::set_translation)
{
    ui->setupUi(this);
    this->setWindowTitle("设置翻译选项");
    //this->updata();//主窗口还未生成 emit 无效

}

set_translation::~set_translation()
{
    delete ui;

}

void set_translation::updata()
{
    //数据库:
    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
      mysql = QSqlDatabase::database("qt_sql_default_connection");
    else
      mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku.db");

    if(mysql.open()==true)
    {
        QSqlQuery query;
        query.exec("select * from appid ");
        if(query.first()==true)
        {
            this->index_this=query.value("tofrom").toInt();//先
            ui->comboBox->setCurrentIndex(this->index_this);
            emit signal_id(index_this);

            ui->TextEdit_appid->setPlainText(query.value("idname").toString());//必须放后面
            ui->TextEdit_miyao->setPlainText(query.value("miyaoname").toString());

        }


        mysql.close();
    }

}

void set_translation::on_comboBox_activated(int index)
{
    qDebug()<<"test="<<index;
    emit signal_id(index);
    //qDebug()<<"index="<<index;
    this->index_this=index;

    //数据库:
    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
      mysql = QSqlDatabase::database("qt_sql_default_connection");
    else
      mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku.db");

    if(mysql.open()==true)
    {
        QString appid;
        QString miyao;
        appid=ui->TextEdit_appid->toPlainText();
        miyao=ui->TextEdit_miyao->toPlainText();

        QSqlQuery query;
        //删除
        query.exec("DROP TABLE appid");
        //
        query.exec("create table appid( id int primary key, idname varchar(200), miyaoname varchar(200),tofrom int )");
        //
        query.exec(QString("insert into appid(id,idname,miyaoname,tofrom) values(0,'%1','%2',%3)").arg(appid).arg(miyao).arg(index_this));

        mysql.close();
    }

}

void set_translation::on_TextEdit_appid_textChanged()
{
    qDebug()<<"on_TextEdit_appid_textChanged";

    //数据库:
    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
      mysql = QSqlDatabase::database("qt_sql_default_connection");
    else
      mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku.db");
    if(mysql.open()==true)
    {
        QString appid;
        QString miyao;
        appid=ui->TextEdit_appid->toPlainText();
        miyao=ui->TextEdit_miyao->toPlainText();

        QSqlQuery query;
        //删除
        query.exec("DROP TABLE appid");
        //
        query.exec("create table appid( id int primary key, idname varchar(200), miyaoname varchar(200),tofrom int )");
        //
        query.exec(QString("insert into appid(id,idname,miyaoname,tofrom) values(0,'%1','%2',%3)").arg(appid).arg(miyao).arg(index_this));

        mysql.close();
    }

}

void set_translation::on_TextEdit_miyao_textChanged()
{
    qDebug()<<"on_TextEdit_miyao_textChanged";
    //数据库:
    QSqlDatabase mysql;
    if(QSqlDatabase::contains("qt_sql_default_connection"))
      mysql = QSqlDatabase::database("qt_sql_default_connection");
    else
      mysql = QSqlDatabase::addDatabase("QSQLITE");
    mysql.setDatabaseName("../myshujuku.db");
    if(mysql.open()==true)
    {
        QString appid;
        QString miyao;
        appid=ui->TextEdit_appid->toPlainText();
        miyao=ui->TextEdit_miyao->toPlainText();

        QSqlQuery query;
        //删除
        query.exec("DROP TABLE appid");
        //
        query.exec("create table appid( id int primary key, idname varchar(200), miyaoname varchar(200),tofrom int )");
        //
        query.exec(QString("insert into appid(id,idname,miyaoname,tofrom) values(0,'%1','%2',%3)").arg(appid).arg(miyao).arg(index_this));

        mysql.close();
    }

}

